﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Ascend.Runtime.Core;

namespace Ascend.Tools.ModelIndexer {
    public partial class Form1 : Form {

        public AscendClassFinder ClassFinder {
            get;
            set;
        }

        public Form1() {
            InitializeComponent();

            this.ClassFinder = new AscendClassFinder();
            this.lbResults.CellClick += new DataGridViewCellEventHandler(lbResults_CellClick);
        }

        public void lbResults_CellClick(object sender, DataGridViewCellEventArgs e) {
            ModelFile f = ClassFinder.Index[this.lbResults.SelectedRows[0].Cells["ColName"].Value.ToString()];
            this.tbSource.Text = f.Source;
        }

        private void tbFolder_TextChanged(object sender, EventArgs e) {
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += new DoWorkEventHandler(Scan);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

            bw.RunWorkerAsync();
        }

        public void Scan(object sender, DoWorkEventArgs e) {
            ClassFinder.Scan(this.tbFolder.Text);
        }

        public void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
            lbResults.DataSource = ClassFinder.Index.Values.ToList();
        }

        private void lbResults_SelectedIndexChanged(object sender, EventArgs e) {
            ModelFile f = ClassFinder.Index[this.lbResults.SelectedRows[0].Cells[0].Value.ToString()];
            this.tbSource.Text = f.Source;
        }

        private void btnFind_Click(object sender, EventArgs e) {
            
        }
    }
}
